/*  This file is part of Paxer.

    CASpp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    CASpp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with CASpp.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2014 Jérome LAURENS

    See https://code.google.com/p/idalgo/

*/

#include <iostream>
#include <paxer/parser.h>
#include <paxer/scanner.h>
#include "driver.h"

PAXER_NS_USING

Driver::Driver ()
  : __super()
  , trace_scanning_m (false)
  , trace_parsing_m (false) {
  variables["one"] = 1;
  variables["two"] = 2;
}

Driver::~Driver () {
}

int Driver::parse (const std::string & input) {
    return parse(input.c_str());
}

int Driver::parse (const QString &input) {
    std::string s;
    QByteArray bra = input.toUtf8();
    return parse(bra.data());
}

bool Driver::traceScanning() {
    return trace_scanning_m;
}
void Driver::setTraceScanning(bool yorn) {
    trace_scanning_m= yorn;
}
bool Driver::traceParsing() {
    return trace_parsing_m;
}
void Driver::setTraceParsing(bool yorn) {
    trace_parsing_m= yorn;
}

void
Driver::error (const int l, const std::string& m) const {
  std::cerr << l << ": " << m << std::endl;
}

void
Driver::error (const std::string& m) const {
  std::cerr << m << std::endl;
}

int Driver::parse (const char * input) {
    scanner_m.reset(new yyscan_t);
    //yyscan_t yyg = (yyscan_t)scanner_m.get();
    //yy_flex_debug = trace_scanning_m;
    void * buffer = 0;//CASppIO_scan_string(input);
    // exception safe, delete buffer at end of scope
    boost::shared_ptr<void> guard(buffer, Driver::deleteBuffer);
    //CASppIO::Parser parser(*this);
    //parser.set_debug_level(trace_parsing_m);
    return 0;//parser.parse();
}
void Driver::deleteScanner(yyscan_t * scan_p) {
    if (scan_p) {
//        CASppIO_lex_destroy(scan_p);
    }
}
void Driver::deleteBuffer(void * buffer) {
    if (buffer) {
//        CASppIO__delete_buffer((YY_BUFFER_STATE)buffer);
    }
}
const std::string Driver::description() const {
    return "COUCOUROUCOUCOU";
}
Driver & Driver::operator=(const Driver & other) {
    if (this != &other) {
        trace_scanning_m = other.trace_scanning_m;
        trace_parsing_m = other.trace_parsing_m;
        scanner_m = other.scanner_m;
    }
    return *this;
}

Driver::__super::element_type & Driver::nodeRef() {
    return __super::element();
}

void Driver::prepareParse() {
    nodeRef() = __super::element_type();
}

void Driver::accept(__super::element_type & theNodeRef) {
#ifndef NDEBUG
    std::cout<<"Driver is accepting:"<<std::endl;
    std::cout << (theNodeRef.get()?theNodeRef->prettyType():"None");
    std::cout << ", ";
    std::cout << (theNodeRef.get()?theNodeRef->description():"None") << std::endl;
#endif
    nodeRef() = theNodeRef;
#ifndef NDEBUG
    std::cout<<"Done: driver has accepted:"<<std::endl;
    std::cout << (nodeRef().get()?nodeRef()->prettyType():"None");
    std::cout << ", ";
    std::cout << (nodeRef().get()?nodeRef()->description():"None") << std::endl;
#endif
}
